<?php

class debug extends Controller {

	function index() {
		$db = new Database();
		$tables = $db->getAll("SHOW TABLES");
		foreach( $tables as $table ) {
			$table = each( $table );
			$table = $table['value'];
			echo "<a href=\"/debug/view/$table\">" . $table . '</a><br />';
		}
	}
	
	function view($table) {
		$db = new Database();
		$columns = $db->getAll("SHOW COLUMNS FROM $table");
		$rows = $db->getAll("SELECT * FROM $table");
		echo '<table><tr style="border: 1px solid #000;">';
		foreach( $columns as $column ) {
			echo '<td style="border-bottom: 1px solid #000; padding-right: 20px;">' . $column['Field'] . '</td>';
		}
		echo '</tr>';
		foreach( $rows as $row ) {
			echo '<tr>';
			foreach( $row as $cell ) {
				echo '<td style="padding-right: 20px;">' . $cell. '</td>';
			}
			echo '</tr>';
		}
		echo '</table>';
	}
	
	function setlastvisit() {
		$db = new Database();
		if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			$db->execute("UPDATE members SET date = ? WHERE members_id = ?",
				array( $_POST['date'], $_POST['id'] )
			);
		}
		$columns = $db->getAll("SHOW COLUMNS FROM members");
		$rows = $db->getAll("SELECT 
			m.members_id,	 CONCAT(u.lastname , ' ',  u.firstname),	o.name,	m.date,	m.banned
		 FROM members m inner join organization o ON o.org_id = m.org_id inner join user u ON m.stud_no = u.stud_no");
		echo '<table><tr style="border: 1px solid #000;">';
		foreach( $columns as $column ) {
			echo '<td style="border-bottom: 1px solid #000; padding-right: 20px;">' . $column['Field'] . '</td>';
		}
		echo '</tr>';
		foreach( $rows as $row ) {
			echo '<tr>';
			foreach( $row as $cell ) {
				echo '<td style="padding-right: 20px;">' . $cell. '</td>';
			}
			echo '</tr>';
		}
		echo '</table>';
?> 
<form action="/debug/setlastvisit" method="post">
	<input type="text" name="id" value="members_id to change (hindi ito student_number)" />
	<input type="text" name="date" value="YYYY-MM-DD H:M:S" />
	<input type="submit" value="change" />
</form>
<?php
	}

	function mail() {
		//Create the Transport
		$transport = Swift_SmtpTransport::newInstance('mail.steadfast.net', 25);

		//Create the Mailer using your created Transport
		$mailer = Swift_Mailer::newInstance($transport);
		$message = Swift_Message::newInstance('Wonderful Subject')
		  ->setFrom(array('dijiyd@dijiyd.com' => 'John Doe'))
		  ->setBcc(array('dijiyd@gmail.com', 'music_lover_372@yahoo.com',))
		  ->setBody('Here is the message itself')
		  ;
		  
		//Send the message
		$result = $mailer->send($message);
		var_dump( $result );
	}

	function mailinglist($param) {
		$memberModel = new member();
		var_dump( $memberModel->getMailAddresses($param) );
	}
	
}